package com.changgou.goods.controller;
import com.changgou.entity.CustomException;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import com.changgou.util.PinYinUtils;
import com.github.pagehelper.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/brand")
@Api(tags = "操作商品品牌的接口")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询品牌全部数据")
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "品牌主键",paramType = "path",required = true,dataType = "Integer")
    })
    public Result findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }


    /***
     * 新增数据
     * @param brand
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增品牌")
    public Result add(@RequestBody Brand brand){

        //品牌名不能为空
        if (StringUtils.isEmpty(brand.getName())){
            throw new CustomException(10086,"品牌名不能为空");
        }
        //品牌首字母没有的话,自动填充首字母
        if (StringUtils.isEmpty(brand.getLetter())){
            brand.setLetter(PinYinUtils.getFirstLetter(brand.getName()));
        }

        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    @ApiOperation(value = "修改品牌")

    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    @ApiOperation(value = "根据id删除品牌数据")
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    @ApiOperation(value = "条件查询品牌数据")
    public Result findList(@RequestParam Map searchMap){
        List<Brand> list = brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    @ApiOperation("条件查询品牌数据实现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "品牌名称"),
            @ApiImplicitParam(name = "letter", value = "品牌首字母"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "当前展示条数")
    })
    public Result findPage(@ApiParam(hidden = true) @RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Brand> pageList = brandService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根剧分类名称查询商品品牌列表
     * @param categoryName
     * @return
     */
    @GetMapping("/category/{categoryName}")
    public Result findBrandListByCategoryName(@PathVariable("categoryName") String categoryName) {
        List<Map> brandList = brandService.findBrandListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }

}
